package org.feifei.user.controller;

import io.swagger.annotations.ApiOperation;
import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.util.CommonResult;
import org.feifei.user.dto.UserDTO;
import org.feifei.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author forever
 * @Description: 处理验证码的Controller
 * @date 2021/12/07 18:14
 */
@RestController
//@CrossOrigin(origins = "http://localhost", maxAge = 3600)
@RequestMapping("/verifycode")
public class VerifyCodeController {

    @Resource
    private UserService userService;

    /**
     * 获取前端请求验证码的接口
     * @param map
     * @return
     */
    @PostMapping("/smsCode")
    public CommonResult sendSmsCode(@RequestBody Map<String, Object> map) {
        try {
            String phone = (String) map.get("phone");
            userService.verifyCode(phone);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure("发送失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 用户通过手机绑定当前微信用户
     * @param
     * @return
     */
    @ApiOperation("用户通过手机号码注册")
    @PostMapping("/binderCode")
    public CommonResult BinderWXByPhone(@RequestBody Map<String, Object> map) {
        try {
            String phone = (String) map.get("phone");
            userService.bindUserByPhone(phone);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure("注册失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("系统错误" + e.getMessage());
        }
    }

    /**
     * 获取前端请求验证码的接口
     * @param map
     * @return
     */
    @PostMapping("/phoneLoginCode")
    public CommonResult phoneLoginCode(@RequestBody Map<String, Object> map) {
        try {
            String phone = (String) map.get("phone");
            userService.verifyCodeLoginByPhone(phone);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure("发送失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

}
